package net.huashitong.supervision.task.dao;

import com.sirdc.modules.core.dao.GenericQuery;
import com.sirdc.modules.core.dao.StringDao;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.supervision.task.entity.TimeLimit;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: ling
 * Date: 2019/9/16
 * Time: 20:39
 * To change this template use File | Settings | File Templates.
 * Description:
 */
@Repository
public class TimeLimitDao extends StringDao<TimeLimit> {
    //返回未结束的任务 1 是未结束， 0 已结束
    public List<TimeLimit> queryState(String state) {
        GenericQuery query = createQuery("obj");
        query.append(" and del is null ");
        query.append(" and state =:state").setParam("state",state);
        return query.listResult();
    }

    public List<TimeLimit> getBySubId(String taskSubId) {
        GenericQuery query = createQuery("obj");
        query.append(" and del is null ");

        query.append(" and taskSubId =:taskSubId").setParam("taskSubId",taskSubId);
        return query.listResult();
    }
    public List<TimeLimit> getbyTaskId(String specialId,String subId,String unitId) {
        GenericQuery query = createQuery("obj");
        query.append(" and del is null ");

        if(StringUtils.isNotBlank(specialId)){
        query.append(" and specialTaskId =:specialTaskId").setParam("specialTaskId",specialId);
        }
        if(StringUtils.isNotBlank(subId))
        {
            query.append(" and taskSubId =:taskSubId").setParam("taskSubId",subId);
        }
        if(StringUtils.isNotBlank( unitId))
        {
            query.append(" and taskUnitId =:taskUnitId").setParam("taskUnitId",unitId);
        }
        query.append(" and state ='1'");
        return query.listResult();
    }
    public List<TimeLimit> getBySubIdDel(String taskSubId) {
        GenericQuery query = createQuery("obj");
        query.append(" and del =:del ").setParam("del","0" );

        query.append(" and taskSubId =:taskSubId").setParam("taskSubId",taskSubId);
        return query.listResult();
    }



    public void deleteBySubId(String subId) {
        StringBuffer hql = (new StringBuffer("delete from TimeLimit")).append(" obj where obj.taskSubId=:taskSubId");
       this.create(hql.toString()).setParam("taskSubId", subId).executeUpdate();
    }
}
